<?php

namespace App\Http\Controllers;

use App\Lib\ApiResponse;
use App\Models\Staff;
use App\Repositories\FootWorkRepository;
use App\Repositories\ManagerRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ManageController extends Controller
{
    protected $manageRepository;

    /**
     * @param ManagerRepository $manageRepository
     */
    public function __construct(ManagerRepository $manageRepository)
    {
        $this->manageRepository = $manageRepository;
    }

    public function reportList(Request $request)
    {
        $mall_id = Auth::guard('admin')->user()->mall_id;
        if($request->isMethod('post')){
            $data = $this->manageRepository->getReportList($mall_id);
            return ApiResponse::success([
                'total' => $data['total'],
                'items' => $data['data'],
            ],0);
        }
        return view('manage.report_list');
    }

    public function reportSync(Request $request)
    {
        $report_id = $request->input('id');
        if(empty($report_id)){
            return ApiResponse::failJson('参数错误');
        }

        $ret = (new FootWorkRepository())->syncTofwlab(['id' => $report_id]);
        if($ret['status'] != ApiResponse::CODE_SUCCESS){
            return ApiResponse::failJson($ret['message']);
        }

        return ApiResponse::successJson();
    }

    public function staffList(Request $request)
    {
        if($request->isMethod('post')){
            $data = $this->manageRepository->getStaffList();
            return ApiResponse::success([
                'total' => $data['total'],
                'items' => $data['data'],
            ],0);
        }
        return view('manage.staff_list');
    }

    public function editStaff(Request $request)
    {
        $data = $request->all();
        if(empty($data['id']) || empty($data['field'])){
            return ApiResponse::failJson('参数错误');
        }
        $staff = Staff::query()->find($data['id']);
        if(empty($staff)){
            return ApiResponse::failJson('管理不存在');
        }

        switch ($data['field']){
            case 'mall_id':
                if(!is_numeric($data['val'])){
                    return ApiResponse::failJson('参数错误！');
                }
                $staff->mall_id = $data['val'];
                $staff->save();
                break;
            case 'admin_type':
                if(!in_array($data['val'],array('0','1','2'))){
                    return ApiResponse::failJson('参数错误!');
                }
                $staff->admin_type = $data['val'];
                $staff->save();
                break;
        }

        return ApiResponse::successJson();
    }

    public function userList(Request $request)
    {
        $mall_id = Auth::guard('admin')->user()->mall_id;
        if($request->isMethod('post')){
            $data = $this->manageRepository->getWxUserList($mall_id);
            return ApiResponse::success([
                'total' => $data['total'],
                'items' => $data['data'],
            ],0);
        }
        return view('manage.user_list');
    }

    public function reportSetting(Request $request)
    {
        $mall_id = Auth::guard('admin')->user()->mall_id;
        if($request->isMethod('post')){
            return $this->manageRepository->saveReportSetting($mall_id);
        }

        $serve_url = route('wx.notify',$mall_id);
        $setting = $this->manageRepository->getSetting($mall_id);
        return view('manage.setting',compact('setting','serve_url'));
    }

    public function logout()
    {
        Auth::guard('admin')->logout();

        return redirect('/');
    }

}
